//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.wuzuqing.android.eightdiagrams.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class JsonParserUtil {
    private static Gson gson = (new GsonBuilder()).create();

    public JsonParserUtil() {
    }

    public static <T> T parseJsonObject(String json, Class<T> clz) {
        return gson.fromJson(json, clz);
    }

    public static <T> T parseJsonObject(JsonElement json, Class<T> clz) {
        return gson.fromJson(json, clz);
    }

    public static <T> T[] parseJsonArray(String json, Class<T> clz) {
        T[] result = (T[]) gson.fromJson(json, (new TypeToken<T[]>() {
        }).getType());
        return result;
    }

    public static <K, V> Map<K, V> parseJsonMap(String json, Class<K> keyType, Class<V> valueType) {
        Type type = new ParameterizedTypeImpl(Map.class, new Class[]{keyType, valueType});
        Map<K, V> result = (Map)gson.fromJson(json, type);
        return result;
    }

    public static String toJson(Object obj) {
        try {
            return gson.toJson(obj);
        } catch (Exception var2) {
            return "{}";
        }
    }

    public static <T> List<T> parseJsonList(String json, Class<T> clz) {
        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(json);
        JsonArray array = element.getAsJsonArray();
        List<T> data = new ArrayList();
        Iterator var6 = array.iterator();

        while(var6.hasNext()) {
            JsonElement jo = (JsonElement)var6.next();
            data.add(gson.fromJson(jo, clz));
        }

        return data;
    }

    public static <T> List<T> parseJsonList(JsonArray array, Class<T> clz) {
        List<T> data = new ArrayList();
        Iterator var3 = array.iterator();

        while(var3.hasNext()) {
            JsonElement jo = (JsonElement)var3.next();
            data.add(parseJsonObject(jo, clz));
        }

        return data;
    }

    public static class ParameterizedTypeImpl implements ParameterizedType {
        private final Class raw;
        private final Type[] args;

        public ParameterizedTypeImpl(Class raw, Type[] args) {
            this.raw = raw;
            this.args = args != null ? args : new Type[0];
        }

        public Type[] getActualTypeArguments() {
            return this.args;
        }

        public Type getRawType() {
            return this.raw;
        }

        public Type getOwnerType() {
            return null;
        }
    }

    private static class NumberTypeAdapter implements JsonSerializer<Number> {
        private NumberTypeAdapter() {
        }

        public JsonElement serialize(Number src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src);
        }
    }
}
